package com.woniu.adopt.web;

import com.github.pagehelper.PageInfo;
import com.woniu.adopt.entity.Pet;
import com.woniu.adopt.entity.RescueStation;
import com.woniu.adopt.entity.Role;
import com.woniu.adopt.result.ResultResponse;
import com.woniu.adopt.service.OssUploadService;
import com.woniu.adopt.service.PetService;
import com.woniu.adopt.vo.EchartsVo;
import com.woniu.adopt.vo.PetVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Api(value = "本接口是操作宠物管理功能", description = "宠物管理接口")
@RestController
@RequestMapping("pet")
public class PetController {

//    @Value("${imgPath}")
//    private String imgPath;

//    @Autowired
//    OssUploadService uploadService;

    @Autowired
    private PetService petService;

    @ApiOperation(value = "宠物列表分页")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "int", name = "now", value = "查询的当前页", required = true, defaultValue = "1"),
            @ApiImplicitParam(paramType = "query", dataType = "int", name = "size", value = "每页显示多少条", required = true, defaultValue = "3"),
            @ApiImplicitParam(paramType = "query", dataType = "string", name = "name", value = "宠物名称", required = false)
    })
    @GetMapping("/list")
    public ResultResponse<PageInfo<PetVo>> plist(@RequestParam(value = "now", defaultValue = "1") Integer now,
                                                 @RequestParam(value = "size", defaultValue = "3") Integer size,
                                                 String name) {
        ResultResponse<PageInfo<PetVo>> result = null;
        try {
            PageInfo<PetVo> petPageInfo = petService.queryByPage(now, size, name);
            result = new ResultResponse<>(200, "查询宠物成功!", petPageInfo);
        } catch (RuntimeException e) {
            e.printStackTrace();
            result = new ResultResponse<>(201, "查询宠物失败!");
        }
        return result;
    }

    @ApiOperation(value = "根据id查询宠物")
    @GetMapping("/findPetByKey")
    public ResultResponse<PetVo> queryPetByKey(@RequestParam(value = "id") Integer id) {
        ResultResponse<PetVo> result = null;
        try {
            PetVo pet = petService.queryPetByKey(id);
            result = new ResultResponse<>(200, "查询宠物成功!", pet);
        } catch (RuntimeException e) {
            e.printStackTrace();
            result = new ResultResponse<>(201, "查询宠物失败!");
        }
        return result;
    }

    @ApiOperation(value = "修改宠物")
    @PutMapping("/modify")
    public ResultResponse<Void> modify(@RequestBody PetVo pet) {
        ResultResponse<Void> result = null;
        try {
            pet.setUpdateTime(new Date());
            petService.modify(pet);
            result = new ResultResponse<>(200, "修改宠物成功!");
        } catch (RuntimeException e) {
            e.printStackTrace();
            result = new ResultResponse<>(201, "修改宠物失败!");
        }
        return result;
    }

    @ApiOperation(value = "新增宠物")
    @PostMapping("/add")
    public ResultResponse<Void> add(@RequestBody Pet pet) {
        ResultResponse<Void> result = null;
        try {
            pet.setInsertTime(new Date());
            System.out.println(pet);
            petService.add(pet);
            result = new ResultResponse<>(200, "添加宠物成功!");
        } catch (RuntimeException e) {
            e.printStackTrace();
            result = new ResultResponse<>(201, "添加宠物失败!");
        }
        return result;
    }

    @ApiOperation(value = "删除宠物")
    @DeleteMapping("/del/{id}")
    public ResultResponse<Void> del(@PathVariable("id") Integer id) {
        ResultResponse<Void> result = null;
        try {
            petService.removeByKey(id);
            result = new ResultResponse<>(200, "删除宠物成功!");
        } catch (RuntimeException e) {
            e.printStackTrace();
            result = new ResultResponse<>(201, "删除宠物失败!");
        }
        return result;
    }

    @ApiOperation(value = "查询所有救助站")
    @GetMapping("/allRescueStation")
    public ResultResponse<List<RescueStation>> queryAllRescueStation() {
        ResultResponse<List<RescueStation>> result = null;
        try {
            List<RescueStation> rescueStations = petService.queryAllRescueStation();
            result = new ResultResponse<>(200, "查询所有救助站成功!", rescueStations);
        } catch (RuntimeException e) {
            e.printStackTrace();
            result = new ResultResponse<>(201, "查询所有救助站失败!");
        }
        return result;
    }

    @ApiOperation(value = "查询浏览量前十宠物")
    @GetMapping("/getViews")
    public ResultResponse<List<EchartsVo>> getViews() {
        ResultResponse<List<EchartsVo>> result = null;
        try {
            List<EchartsVo> list = petService.queryViews();
            result = new ResultResponse<>(200, "查询成功!", list);
        } catch (RuntimeException e) {
            e.printStackTrace();
            result = new ResultResponse<>(201, "查询失败!");
        }
        return result;
    }
//    @PostMapping("/uploadImg")
//    public ResultResponse<String> uploadImg(@RequestParam(value = "uploadImg") MultipartFile multipartFile) {
//        ResultResponse<String> result = null;
//        try{
//            //获取上传的文件名字
//            String filename=multipartFile.getOriginalFilename();
//            filename = UUID.randomUUID().toString().replaceAll("-","")+filename.substring(filename.lastIndexOf("."));
//            File f = new File(imgPath);
//            if(!f.exists()){
//                f.mkdirs();
//            }
//            File file = new File(f,filename);
//            multipartFile.transferTo(file);
//            result = new ResultResponse<>(200,"上传成功",filename);
//        }catch (Exception e){
//            e.printStackTrace();
//            result = new ResultResponse<>(200,"上传失败");
//        }
//        return result;
//    }
//    @PostMapping("/upload")
//    public ResultResponse<String> upload(@RequestParam("file") MultipartFile multipartFile){
//        ResultResponse<String> resultResponse = null;
//        String result = uploadService.uploadImg(multipartFile);
//        System.out.println(result);
//        resultResponse = new ResultResponse<>(200,"上传成功",result);
//        return resultResponse;
//    }
}
